<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/1/24
 * Time: 4:52 PM
 */

namespace api\controllers;


use api\models\Good;
use api\models\Order;
use yii\helpers\ArrayHelper;
use yii\web\BadRequestHttpException;
use yii\web\ServerErrorHttpException;

class GoodController extends BaseController
{
    public $modelClass = "api\models\Good";

    public function actionColors()
    {
        $model = Good::findAll(['type' => Good::TYPE_COLORS]);

        return $model;
    }


    public function actionServices()
    {
        $parttime_vip         = Good::findOne(['code' => "part_time_vip_month"]);
        $parttime_check_sigup = Good::findOne(['code' => "parttime_sign_up_num_add"]);
        $sms_notice_mission   = Good::findOne(['code' => "mission_sms_notice"]);

        return [
            [
                'title'       => $parttime_check_sigup->title,
                'description' => $parttime_check_sigup->description,
                'good_id'     => $parttime_check_sigup->id,
                'price'       => $parttime_check_sigup->price,
            ],
            [
                'title'       => $parttime_vip->title,
                'description' => $parttime_vip->description,
                'good_id'     => $parttime_vip->id,
                'price'       => $parttime_vip->price,
            ],
            [
                'title'       => $sms_notice_mission->title,
                'description' => $sms_notice_mission->description,
                'good_id'     => $sms_notice_mission->id,
                'price'       => $sms_notice_mission->price,
            ],
        ];
    }


    /**
     *
     * $good_id
     * $num
     *
     *
     * $type
     *
     * const TYPE_PART_TIME_POST = 1;
     * const TYPE_PART_TIME_VIP = 2;
     * const TYPE_MISSION_POST = 3;
     * const TYPE_MISSION_SIGN_UP_CHARGE = 4;
     * const TYPE_MISSION_SMS = 5;
     * const TYPE_PART_TIME_SIGN_UP_CHARGE = 6;
     * const TYPE_TAKE_MISSION = 7;
     *
     * @throws \yii\web\ServerErrorHttpException
     */
    public function actionBuy()
    {
        $post  = \Yii::$app->getRequest()->getBodyParams();
        $map   = [
            'mission_sms_notice'       => Order::TYPE_MISSION_SMS,
            'parttime_sign_up_num_add' => Order::TYPE_PART_TIME_SIGN_UP_CHARGE,
            'part_time_vip_month'      => Order::TYPE_PART_TIME_VIP
        ];
        $order = null;
        if (isset($post['good_id'], $post['num'])) {
            $good = Good::findOne($post['good_id']);
            $type = isset($map[$good->code]) ? $map[$good->code] : null;
//            var_dump($map[$good->code]);
            if ( ! empty($type)) {
                if ($type == Order::TYPE_PART_TIME_SIGN_UP_CHARGE && ! isset($post['job_id'])) {
                    throw new BadRequestHttpException("缺少job_id");
                }
                $order = Order::generateOrderByGoodId($post['good_id'], $type, $post['num'], uniqid(),
                    isset($post['job_id']) ? $post['job_id'] : null);
//                var_dump($order);
            } else {
                throw new ServerErrorHttpException("非法商品");
            }
        } else {
            throw new BadRequestHttpException('数据非法');
        }
        return ["out_trade_no" => $order->order_id];
    }


}